package com.music.controller;

import com.music.dto.Result;
import com.music.entity.Message;
import com.music.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
@Slf4j
public class MessageController {
    @Autowired
    private WebSocket webSocket;
    @Autowired
    private MessageService messageService;
    @PostMapping("/getByMyUserId")
    public @ResponseBody Result getByMyUserId(@RequestParam String userId) {
        Result result = new Result();
        List<Message> messages=messageService.getByMyUserId(userId);
        if(!messages.isEmpty()){
            result.ok(messages);
        }else{
            result.fail(0,"没有相关数据");
        }
        return result;
    }

    @PostMapping("/addMessage")
    public @ResponseBody Result addMessage(@RequestParam String userId, @RequestParam String commentId,@RequestParam String otherUserId,@RequestParam String url,String commentText) {

        webSocket.sendMessage("有人回复了你的评论:"+commentText+","+otherUserId);
        Result result = new Result();
        Message message=new Message();
        message.setMessageStatus("1");
        message.setUrl(url);
        message.setMessageText(commentId);
        message.setOtherUserId(otherUserId);
        message.setMyUserId(userId);
        boolean flag=messageService.addMessage(message);
        if(flag){
            result.ok(true);
        }else{
            result.fail(0,"插入失败");
        }
        return result;
    }

    @PostMapping("/updataMessage")
    public @ResponseBody Result updataMessage(@RequestParam String userId) {
        Result result = new Result();
        boolean flag=messageService.updataMessage(userId);
        if(flag){
            result.ok(true);
        }else{
            result.fail(0,"数据错误");
        }
        return result;
    }
}
